package com.seagox.desk.remote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seagox.desk.remote.entity.VersionRecord;
import com.seagox.desk.remote.mapper.VersionRecordMapper;
import com.seagox.desk.remote.service.IVersionRecordService;
import com.seagox.desk.common.ResultData;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VersionRecordService implements IVersionRecordService {
    
    @Autowired
    private VersionRecordMapper versionRecordMapper;

	@Override
	public ResultData check(String platform, String arch, String version) {
		LambdaQueryWrapper<VersionRecord> qw = new LambdaQueryWrapper<>();
    	qw.eq(VersionRecord::getPlatform, platform)
    	.eq(VersionRecord::getArch, arch).orderByDesc(VersionRecord::getCreateTime);
    	List<VersionRecord> list = versionRecordMapper.selectList(qw);
    	if(list.size() > 0) {
    		VersionRecord versionRecord = list.get(0);
    		if(versionRecord.getVersion().equals(version)) {
    			return ResultData.success(null);
    		} else {
    			return ResultData.success(versionRecord);
    		}
    	} else {
    		return ResultData.success(null);
    	}
	}

	@Override
	public ResultData download(String platform, String arch) {
		LambdaQueryWrapper<VersionRecord> qw = new LambdaQueryWrapper<>();
    	qw.eq(VersionRecord::getPlatform, platform)
    	.eq(VersionRecord::getArch, arch).orderByDesc(VersionRecord::getCreateTime);
    	List<VersionRecord> list = versionRecordMapper.selectList(qw);
    	if(list.size() > 0) {
    		VersionRecord versionRecord = list.get(0);
    		return ResultData.success(versionRecord);
    	} else {
    		return ResultData.success(null);
    	}
	}
}
